package com.bdqn.t350.util;

import com.bdqn.t350.pojo.SysUser;
import com.bdqn.t350.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

public class SysInterceptor extends HandlerInterceptorAdapter {


    @Autowired
    SysUserService sysUserService;

    //进入某个方法之前需要去判断权限
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("已经进入了自定义的拦截器");

        ApplicationContext apppApplicationContext = (ApplicationContext) request.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
        String[] beanDefinitionNames = apppApplicationContext.getBeanDefinitionNames();
        Arrays.sort(beanDefinitionNames);
        System.out.println(Arrays.asList(beanDefinitionNames));

        List<SysUser> listBiz = sysUserService.getListBiz();
        System.out.println(listBiz);


        HttpSession session = request.getSession();
        if (session.getAttribute("sysUser") == null) {
            throw new RuntimeException("登录会话已经过期，哈哈!");
        } else {
            return super.preHandle(request, response, handler);
        }
    }

/*    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        super.afterCompletion(request, response, handler, ex);
    }*/
}
